//
// Created by gogoqiu on 2021/10/21.
//

#include "RepoPush.h"
#include <git2.h>

/*
Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if( !new File( PRIKEY ).exists()){
                onSessionEvent.OnInitFail( "PRIKEY not existed!" );
                return;
            }

            if( !new File( KNOWN_HOSTS ).exists()){
                onSessionEvent.OnInitFail( "KNOWN_HOSTS not existed!" );
                return;
            }
            try {
                LogFile.Write( context, "MYLOG","执行线程已启动" );
                requestGitRepoIp();
            }catch ( IOException e){
                onSessionEvent.OnQueryIpError( e.getMessage() );
                return;
            }
            File source = new File(real_mylog_path);
            File dest = new File(fake_mylog_path);
            try {
                FileInputStream fileInputStream = new FileInputStream(source);
                FileOutputStream fileOutputStream = new FileOutputStream(dest);
                if ( !dest.exists() )
                    dest.createNewFile();
                byte[] buf = new byte[1024];
                long len;
                //copy source to dest
                while ((len = fileInputStream.read(buf)) != -1) {
                    fileOutputStream.write(buf, 0, (int) len);
                }
                fileOutputStream.close();

                onSessionEvent.OnCopyOk();
                //装填好
                session = GitSession.COPY;
                Status();
            }catch ( IOException e) {
                onSessionEvent.OnCopyFail();
                Log.v( TAG, e.getMessage()+","+ real_mylog_path + "," + fake_mylog_path );
                return;
            }

            Git git1 = null;
            try{
                git1 = Git.open(new File(REPO));

                // Stage all files in the repo including new files
                git1.add().addFilepattern(".").call();

                // and then commit the changes.


//git1.getRepository();
//git1.diff().setNewTree()
String s = gitDiff( git1 );
Log.v( TAG, s );
git1.commit()
.setAll(true)
.setMessage( s )
.call();
onSessionEvent.OnCommitOk();
session = GitSession.COMMIT;
Status();
}catch (Exception e) {
onSessionEvent.OnCommitFail();
Log.v( TAG, "OnCommitFail"+ e.getMessage() );
return;
}
try {
git1.push().setRemote(GIT_URL)
.setPushAll()
.setPushTags()
.setProgressMonitor(gitProgressMonitor)
.setTransportConfigCallback(transport -> {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
})
.call();
session = GitSession.PUSH;
Status();
onSessionEvent.OnPushOk();
} catch (Exception e) {
onSessionEvent.OnPushFail();
Log.v( TAG, "OnPushFail:" + e.getMessage() );
return;
}
try{
if ( dest.length() > 1024 * 100 ) {
//文件长度超过厥值
LogFile.Write( context, "", "文件长度超越100k, 启动上传清理转换" );
//清空库外实际文件
clearFile();
//update the new slot file name, fake_mylog_path haven't update
SettingsActivity.set_fake_mylog_filename(context);
//fake mylog post
postRealMyLogToWebServer();
session = GitSession.POST;
Status();
}else{
LogFile.Write( context, "","日志文件长度: "+ dest.length() );
}
}catch (IOException e){
onSessionEvent.OnPostWebFail();
return;
}
session = GitSession.ALL_DONE;
result = true;
Status();
}
};
 */
char* gitMessage(){

}